#coding=utf-8

from uliweb.orm import *
from uliweb.utils.common import get_var
import datetime

def get_modified_user():
    from uliweb import request
    
    return request.user.id

#class Test(Model):
#    __verbose_name__ = u'功能测试'
#    
#    test_id = Field(CHAR, verbose_name='功能测试编号', max_length=18, required=True, unique=True, index=True)
#    sequence_number = Field(str, verbose_name='序号', max_length=255, required=True)
#    
#    system = Reference('system', verbose_name='所属系统', required=True, collection_name='sys_tests')
#    function_name = Field(str, verbose_name='功能名称', max_length=255, required=True)
#    function_code = Field(str, verbose_name='功能代码', max_length=255, required=True)
#    function_type = Field(CHAR, verbose_name='功能类型', max_length=1, choices=get_var('PARA/FUNCTION_TYPE'), required=True)
#    
#    req_id = Field(str, verbose_name='准开发需求编号', max_length=255, required=True)
#    req_title = Field(str, verbose_name='准开发需求名称', max_length=255, required=True)
#    business_rules = Field(str, verbose_name='业务规则', max_length=255, required=True)
#    
#    change_status = Field(CHAR, verbose_name='变动状态', max_length=1, choices=get_var('PARA/CHANGE_STATUS'), required=True)
#    review_status = Field(CHAR, verbose_name='评审状态', max_length=1, choices=get_var('PARA/CHANGE_STATUS'), required=True)
#    business_degree = Field(CHAR, verbose_name='业务重要程度', max_length=1, choices=get_var('PARA/BUSINESS_DEGREE'), required=True)
#    
#    trading_route_origin = Field(str, verbose_name='交易路径上游', max_length=255)
#    route_origin_code = Field(str, verbose_name='上游接口代码', max_length=255)
#    trading_route_downstream = Field(str, verbose_name='交易路径下游', max_length=255)
#    route_downstream_code = Field(str, verbose_name='下游接口代码', max_length=255)
#    
#    trading_frequency = Field(bool, verbose_name='是否属于访问频度高的交易') 
#    daily_visit = Field(int, verbose_name='日均访问量')
#    creator_user = Reference('user', verbose_name='创建人', default=get_modified_user, collection_name='creator_tests')
#    
#    using_status = Field(bool, verbose_name='开放使用状态')
#    using_verification_req = Field(bool, verbose_name='开放验密需求')
#    function_point = Field(TEXT, verbose_name='功能测试要点')
#    
#    must_test_function = Field(bool, verbose_name='是否属于应测试功能')
#    
#    subsystem_abbr = Field(str, verbose_name='子系统英文简写', max_length=20)
#    subsystem_name = Field(str, verbose_name='子系统中文名称', max_length=255)
#    modified_date = Field(datetime.datetime, verbose_name='修改时间', auto_now=True, auto_now_add=True)
#    modifield_user = Reference('user', verbose_name='修改人', default=get_modified_user, collection_name='modifield_user')
#    delete_flag = Field(bool, verbose_name='删除标志')
    
#    across_system = Field(bool, verbose_name='是否属于跨系统交易')
#    trading_route = Field(str, verbose_name='交易路径', max_length=255)
#    test_version = Field(CHAR, verbose_name='测试版本', max_length=20)
#    version_date = Field(datetime.date, verbose_name='投产时间', required=True)
#    test_status = Field(CHAR, verbose_name='功能测试执行情况', max_length=1, choices=get_var('PARA/TEST_STATUS'))
#    function_endtime = Field(datetime.date, verbose_name='测试通过时间')
#    lock_flag = Field(bool, verbose_name='测试功能是否锁定')
#    subsystem_abbr = Field(str, verbose_name='子系统英文简写', max_length=20)
#    subsystem_name = Field(str, verbose_name='子系统中文名称', max_length=255)
#    modified_date = Field(datetime.datetime, verbose_name='修改时间', auto_now=True, auto_now_add=True)
#    modifield_user = Reference('user', verbose_name='修改人', default=get_modified_user)
    
   # def __unicode__(self):
    #        return u'%s-%s' % (self.function_name)
#    @classmethod
#    def OnInit(cls):
#        Index('test_indx', cls.c.system, cls.c.function_code, unique=True)
#    
#    def can_modified(self, user):
#        """
#        根据用户身份来判断是否可以修改
#        """
#        if user.is_superuser:
#            return False
#        
#        #条线人员
#        if user.department == '1':
#            return False
#        #开发中心人员
#        elif user.department == '2':
#            if user.is_gm:
#                return user.kfzx.kfzx_systems.has(self.system)
#            else:
#                return user.users_systems.has(self.system)
#        #测试用户        
#        elif user.department == '3':
##            if user.users_systems:
##                return user.users_systems.has(self.system) 
##            else:
#            return True
#
#    class AddForm:
##           fields = [{'name':'Sequence_Number'},'system','function_name',
##            'function_code',
##            'function_type','req_id','req_title','business_rules',
##            'change_status','review_status','business_degree',
##            'trading_route_origin','route_origin_code','trading_route_downstream',
##            'trading_frequency','daily_visit','using_status',
##            'using_verification_req','function_point','must_test_function',
##            'subsystem_abbr','subsystem_name',
##            {'name':'version_date','verbose_name':'投产时间', 'width':80},
##            {'name':'executing_status','verbose_name':'是否执行测试', 'width':80},
##            {'name':'executing_analysis','verbose_name':'执行方式分析', 'width':80},
##            {'name':'function_result','verbose_name':'功能测试结果', 'width':80},
##            ]
#           fields = ['sequence_number','system','function_name',
#             'function_code',
#             'function_type','req_id','req_title','business_rules',
#             'change_status','review_status','business_degree',
#             'trading_route_origin','route_origin_code','trading_route_downstream',
#             'trading_frequency','daily_visit','using_status',
#             'using_verification_req','function_point','must_test_function',
#             'subsystem_abbr','subsystem_name',
##             {'name':'version_date','verbose_name':'投产时间', 'width':80},
##             {'name':'executing_status','verbose_name':'是否执行测试', 'width':80},
##             {'name':'executing_analysis','verbose_name':'执行方式分析', 'width':80},
##             {'name':'function_result','verbose_name':'功能测试结果', 'width':80},
#             ]
#            
#    class EditForm:
#        fields = ['sequence_number','system','function_name',
#          'function_code',
#          'function_type','req_id','req_title','business_rules',
#          'change_status','review_status','business_degree',
#          'trading_route_origin','route_origin_code','trading_route_downstream',
#          'trading_frequency','daily_visit','using_status',
#          'using_verification_req','function_point','must_test_function',
#          'subsystem_abbr','subsystem_name',
#          {'name':'version_date','verbose_name':'投产时间', 'width':80},
#          {'name':'executing_status','verbose_name':'是否执行测试', 'width':80},
#          {'name':'executing_analysis','verbose_name':'执行方式分析', 'width':80},
#          {'name':'function_result','verbose_name':'功能测试结果', 'width':80},
#          ]
#            
#    class DetailView:
#        fields = ['test_id','sequence_number','system','function_name',
#        'function_code',
#        'function_type','req_id','req_title','business_rules',
#        'change_status','review_status','business_degree',
#        'trading_route_origin','route_origin_code','trading_route_downstream',
#        'trading_frequency','daily_visit','using_status',
#        'using_verification_req','function_point','must_test_function',
#        'subsystem_abbr','subsystem_name','creator_user',
#        'modifield_user','modified_date',]
#                
#    class Table:
#        fields = [
#            {'name':'id'},
#            {'name':'test_id'},
#            {'name':'sequence_number'},
#            {'name':'system'},
#            {'name':'function_name'},
#            {'name':'function_code'},
#            {'name':'function_type'},
#            {'name':'req_id'},
#            {'name':'req_title'},
#            {'name':'business_rules'},
#            {'name':'change_status'},
#            {'name':'review_status'},
#            {'name':'business_degree'},
#            {'name':'trading_route_origin'},
#            {'name':'route_origin_code'},
#            {'name':'trading_route_downstream'},
#            {'name':'trading_frequency'},
#            {'name':'daily_visit'},
#            {'name':'using_status'},
#            {'name':'using_verification_req'},
#            {'name':'function_point'},
#            {'name':'must_test_function'},
#            {'name':'subsystem_abbr'},
#            {'name':'subsystem_name'},
#            {'name':'creator_user'},
#            {'name':'modifield_user'},
#            {'name':'modified_date'},
#        ]


#class Test_version(Model):
#    __verbose_name__ = u'功能测试情况表'
#    
#    test = Reference('test', verbose_name='测试功能点')
#    version_date = Field(datetime.date, verbose_name='投产时间', required=True)
#    executing_status = Field(bool, verbose_name='是否执行测试')
#    executing_analysis = Field(CHAR, verbose_name='执行方式分析', max_length=20)
#    function_result = Field(CHAR, verbose_name='功能测试结果', max_length=20)
#    modified_date = Field(datetime.datetime, verbose_name='修改时间', auto_now=True, auto_now_add=True)
#    modifield_user = Reference('user', verbose_name='修改人', default=get_modified_user)
#    
#    class Table:
#        fields = [
#            {'name':'id'},
#            {'name':'test'},
#            {'name':'version_date'},
#            {'name':'executing_status'},
#            {'name':'executing_analysis'},
#            {'name':'function_result'},
#            {'name':'modified_date'},
#            {'name':'modifield_user'},
#            ]
#
#    @classmethod
#    def OnInit(cls):
#        Index('test_version_indx', cls.c.test, cls.c.version_date, unique=True)
#    
#class Test_history(Model):
#    __verbose_name__ = u'功能测试历史表'
#    
#    test_id = Field(CHAR, verbose_name='功能测试编号', max_length=18, required=True, unique=True, index=True)
#    Sequence_Number = Field(str, verbose_name='序号', max_length=255, required=True)
#    
#    system = Reference('system', verbose_name='所属系统', required=True, collection_name='sys_tests_his')
#    function_name = Field(str, verbose_name='功能名称', max_length=255, required=True)
#    function_code = Field(str, verbose_name='功能代码', max_length=255, required=True)
#    function_type = Field(CHAR, verbose_name='功能类型', max_length=1, choices=get_var('PARA/FUNCTION_TYPE'), required=True)
#    
#    req_id = Field(str, verbose_name='准开发需求编号', max_length=255, required=True)
#    req_title = Field(str, verbose_name='准开发需求名称', max_length=255, required=True)
#    business_rules = Field(str, verbose_name='业务规则', max_length=255, required=True)
#    
#    change_status = Field(CHAR, verbose_name='变动状态', max_length=1, choices=get_var('PARA/CHANGE_STATUS'), required=True)
#    review_status = Field(CHAR, verbose_name='评审状态', max_length=1, choices=get_var('PARA/CHANGE_STATUS'), required=True)
#    business_degree = Field(CHAR, verbose_name='业务重要程度', max_length=1, choices=get_var('PARA/BUSINESS_DEGREE'), required=True)
#    
#    trading_route_origin = Field(str, verbose_name='交易路径上游', max_length=255)
#    route_origin_code = Field(str, verbose_name='上游接口代码', max_length=255)
#    trading_route_downstream = Field(str, verbose_name='交易路径下游', max_length=255)
#    route_downstream_code = Field(str, verbose_name='下游接口代码', max_length=255)
#    
#    trading_frequency = Field(bool, verbose_name='是否属于访问频度高的交易') 
#    daily_visit = Field(int, verbose_name='日均访问量')
#    creator_user = Reference('user', verbose_name='创建人', default=get_modified_user, collection_name='creator_tests_his')
#    
#    using_status = Field(bool, verbose_name='开放使用状态')
#    using_verification_req = Field(bool, verbose_name='开放验密需求')
#    function_point = Field(TEXT, verbose_name='功能测试要点')
#    
#    must_test_function = Field(bool, verbose_name=' 是否属于应测试功能')
#    
#    subsystem_abbr = Field(str, verbose_name='子系统英文简写', max_length=20)
#    subsystem_name = Field(str, verbose_name='子系统中文名称', max_length=255)
#    modified_date = Field(datetime.datetime, verbose_name='修改时间', auto_now=True, auto_now_add=True)
#    modifield_user = Reference('user', verbose_name='修改人', default=get_modified_user, collection_name='modifield_user_his')
#    delete_flag = Field(bool, verbose_name='删除标志')
    


class Test_finish_rate(Model):
    __verbose_name__ = u'功能测试通过情况'
    
    system = Field(int, verbose_name='所属系统')
    version_date = Field(datetime.date, verbose_name='投产时间', required=True)
    change_num = Field(float,verbose_name='测试案例变更次数',default=0)
    test_finish_rate = Field(float,verbose_name='测试案例通过比例',default=0)
    req_modified_date =  Field(datetime.date, verbose_name='需求封版时间')
    test_total_num = Field(int,verbose_name='测试案例总数',default=0)
    
    @classmethod
    def OnInit(cls):
        Index('test_finish_rate_indx', cls.c.system, cls.c.version_date, unique=True)
    
class TestPlan(Model):
    test_id = Field(CHAR, max_length=9, verbose_name='测试计划编号', unique=True, index=True)
    requirements = ManyToMany('requirement', verbose_name='准开发需求')
    tasks = ManyToMany('tasks_develop', verbose_name='关联任务')
    tx = Reference('tx', verbose_name='条线')
    kfzx = Reference('kfzx', verbose_name='开发中心')
    depart = Reference('depart', verbose_name='提出方', required=True)
    system = Reference('system', verbose_name='系统', collection_name='sys_test_plans')
    sub_system = Field(str, max_length=30, verbose_name='子系统', hint='填写子系统的英文缩写')
    keywords = Field(str, max_length=120, verbose_name='测试任务关键词')
    project_name = Field(str, max_length=255, verbose_name='项目名称')
    desc = Field(TEXT, verbose_name='测试任务目标和范围概述', required=True)
    version_date = Field(datetime.date, verbose_name='投产时间')
    title = Field(str, max_length=255, verbose_name='测试任务名称')
    type = Field(CHAR, max_length=1, choices=get_var('PARA/TEST_TYPE'), verbose_name='测试内容', required=True)
    content_type = Field(CHAR, max_length=1, choices=get_var('PARA/TEST_CONTENT'), verbose_name='测试类型', required=True)
    mode = Field(CHAR, max_length=1, choices=get_var('PARA/TEST_MODE'), verbose_name='测试实施模式', required=True)
    real_mode = Field(CHAR, max_length=1, choices=get_var('PARA/TEST_MODE'), verbose_name='实际实施模式')
    origin_begin_date = Field(datetime.date, verbose_name='原计划开始时间', required=True)
    origin_end_date = Field(datetime.date, verbose_name='原计划结束时间', required=True)
    plan_begin_date = Field(datetime.date, verbose_name='拟定计划开始时间')
    plan_end_date = Field(datetime.date, verbose_name='拟定计划结束时间')
    real_begin_date = Field(datetime.date, verbose_name='实际测试开始时间')
    real_end_date = Field(datetime.date, verbose_name='实际测试结束时间')
    env_use_begin_date = Field(datetime.date, verbose_name='环境使用开始时间')
    env_use_end_date = Field(datetime.date, verbose_name='环境使用结束时间')
    env_deploy_req_date = Field(datetime.date, verbose_name='环境部署需求提交时间')
    status = Field(CHAR, max_length=1, choices=get_var('PARA/TEST_TASK_STATUS'), verbose_name='测试进度状态')
    test_systems = ManyToMany('system', verbose_name='测试支持系统', collection_name='test_sys_test_plans')
    rel_systems = ManyToMany('system', verbose_name='协同系统', collection_name='rel_sys_test_plans')
    has_env = Field(CHAR, max_length=1, verbose_name='是否具备环境', choices=get_var('PARA/TEST_HAS_ENV'))
    use_kfzx_env = Field(bool, verbose_name='是否使用开发中心环境')
    use_deployed_env = Field(bool, verbose_name='是否使用已分配的集中测试环境')
#    new_env = Field(TEXT, verbose_name='新增的系统及配置')
    final_env = Field(TEXT, verbose_name='计划提供的系统及配置')
#    need_systems = ManyToMany('system', verbose_name='需要提供连接的系统', collection_name='need_sys_test_tasks')
#    cant_systems = ManyToMany('system', verbose_name='无法提供连接的系统', collection_name='cant_sys_test_tasks')
    contactor = Field(str, max_length=255, verbose_name='联系人及方式', required=True)
    test_contactor = Field(str, max_length=255, verbose_name='测试干系人')
    memo = Field(TEXT, verbose_name='备注')
    qc_domain = Field(str, max_length=80, verbose_name='QC域名')
    qc_project = Field(str, max_length=255, verbose_name='QC项目名')
    approve = Field(CHAR, max_length=1, verbose_name='审批结果', choices=get_var('PARA/TEST_APPROVE_STATUS'), default='0')
    modified_date = Field(datetime.datetime, verbose_name='修改时间', auto_now=True, auto_now_add=True)
    modifield_user = Reference('user', verbose_name='修改人', default=get_modified_user, auto=True, auto_add=True)
    submitter = Reference('user', verbose_name='提交人', collection_name='submitter_test_tasks')
    submit_date = Field(datetime.date, verbose_name='提交时间')
    approver = Reference('user', verbose_name='审批人', collection_name='approver_test_tasks')
    approve_date = Field(datetime.date, verbose_name='审批时间')
    create_type = Field(CHAR, max_length=1, verbose_name='创建类型', choices=get_var('PARA/TEST_CREATE_TYPE'))

    class Table:
        fields = [
            {'name':'test_id', 'width':90, 'sortable':True},
            {'name':'approve', 'width':60, 'sortable':True},
            {'name':'tx', 'width':40, 'sortable':True},
            {'name':'kfzx', 'width':60, 'sortable':True},
            {'name':'depart', 'width':100, 'sortable':True},
            {'name':'system', 'width':60, 'sortable':True},
            {'name':'sub_system', 'width':60},
            {'name':'project_name', 'width':200},
#            {'name':'desc', 'width':100},
            {'name':'version_date', 'width':80, 'sortable':True},
            {'name':'title', 'width':200},
            {'name':'type', 'width':60, 'sortable':True},
            {'name':'content_type', 'width':60, 'sortable':True},
            {'name':'mode', 'width':100, 'sortable':True},
            {'name':'real_mode', 'width':100, 'sortable':True},
            {'name':'origin_begin_date', 'width':80},
            {'name':'origin_end_date', 'width':80},
            {'name':'plan_begin_date', 'width':80},
            {'name':'plan_end_date', 'width':80},
            {'name':'real_begin_date', 'width':80},
            {'name':'real_end_date', 'width':80},
            {'name':'env_use_begin_date', 'width':80},
            {'name':'env_use_end_date', 'width':80},
            {'name':'env_deploy_req_date', 'width':80},
            {'name':'status', 'width':90},
            {'name':'has_env', 'width':90, 'sortable':True},
            {'name':'contactor', 'width':200},
            {'name':'test_contactor', 'width':200},
            {'name':'qc_domain', 'width':100},
            {'name':'qc_project', 'width':100},
            {'name':'requirements', 'width':200},
            {'name':'tasks', 'width':200},
        ]
        
    class AddTaskForm:
        fields = ['depart', 'sub_system', 'keywords',
            'desc', 'type', 'mode', 'project_name',
            'content_type',
            'origin_begin_date', 'origin_end_date',
            'env_use_begin_date', 'env_use_end_date',
            'env_deploy_req_date',
            'test_systems', 'rel_systems', 
            'use_kfzx_env', 'use_deployed_env',
            {'name':'new_env', 'verbose_name':'新环境需求'}, 'contactor', 'memo']
        
        layout = [('depart', 'sub_system'),
            ('keywords', 'project_name'),
            'desc', ('type', 'content_type', 'mode'),
            ('origin_begin_date', 'origin_end_date'),
            ('env_use_begin_date', 'env_use_end_date'),
            'env_deploy_req_date',
            'test_systems', 'rel_systems', 
            ('use_kfzx_env', 'use_deployed_env'),
            'new_env', 'contactor', 'memo']
            
    class EditForm:
        fields = ['depart', 'sub_system', 'keywords',
            'planexecutes', 'content_type',
            'desc', 'type', 'mode', 'project_name',
            'origin_begin_date', 'origin_end_date',
            'env_use_begin_date', 'env_use_end_date',
            'env_deploy_req_date',
            'test_systems', 'rel_systems', 
            'use_kfzx_env', 'use_deployed_env',
            'new_env', 'contactor', 'memo']
        layout = [('depart', 'sub_system'), 
            ('keywords', 'project_name'),
            'planexecutes',
            'desc', ('type', 'mode', 'content_type'),
            ('origin_begin_date', 'origin_end_date'),
            ('env_use_begin_date', 'env_use_end_date'),
            'env_deploy_req_date',
            'test_systems', 'rel_systems', 
            ('use_kfzx_env', 'use_deployed_env'),
            'new_env', 'contactor', 'memo']
       
    class EditTaskForm:
        fields = ['depart', 'sub_system', 'keywords',
            'planexecutes',
            'content_type',
            'desc', 'type', 'mode', 'project_name',
            'origin_begin_date', 'origin_end_date',
            'env_use_begin_date', 'env_use_end_date',
            'env_deploy_req_date',
            'test_systems', 'rel_systems', 
            'use_kfzx_env', 'use_deployed_env',
            'new_env', 'contactor', 'memo']
        layout = [('depart', 'sub_system'), 
            ('keywords', 'project_name'),
            'planexecutes',
            'desc', ('type', 'content_type', 'mode'),
            ('origin_begin_date', 'origin_end_date'),
            ('env_use_begin_date', 'env_use_end_date'),
            'env_deploy_req_date',
            'test_systems', 'rel_systems', 
            ('use_kfzx_env', 'use_deployed_env'),
            'new_env', 'contactor', 'memo']
    
    class DetailView:
        fields = [
            'test_id', 'title',
            'desc',
            'type', 'content_type', 'mode',
            'origin_begin_date', 'origin_end_date',
            'plan_begin_date', 'plan_end_date',
            'env_use_begin_date', 'env_use_end_date',
            'env_deploy_req_date',
            'test_systems', 'rel_systems',
            'use_kfzx_env', 'use_deployed_env', {'name':'new_env', 'verbose_name':'新环境需求'}, 
            
            'tx', 'kfzx', 'depart', 'system', 'sub_system', 
            'project_name', 'version_date', 'contactor',
            'tasks', 'requirements',
            'memo',
            'submitter', 'submit_date',
            
            'real_mode', 'plan_begin_date', 'plan_end_date',
            'has_env', 'final_env',
            'approve', 'approver', 'approve_date',
            
            'status', 'real_begin_date', 'real_end_date',
            'test_contactor', 'qc_domain', 'qc_project',
            
            'modified_date', 'modifield_user',
        ]
        
        layout = [
            '-- 测试计划需求 --',
            ('test_id', 'title'),
            'desc',
            ('type', 'content_type', 'mode'),
            ('origin_begin_date', 'origin_end_date'),
            ('plan_begin_date', 'plan_end_date'),
            ('env_use_begin_date', 'env_use_end_date'),
            'env_deploy_req_date',
            ('test_systems', 'rel_systems'),
            ('use_kfzx_env', 'use_deployed_env'),
            'new_env',
            
            '-- 基本信息 --',
            ('tx', 'kfzx', 'depart', 'system', 'sub_system'), 
            ({'name':'project_name', 'colspan':2}, 'version_date', 'contactor'),
            ('tasks', 'requirements'),
            'memo',
            ('submitter', 'submit_date'),
            
            '-- 审批信息 --',
            ('real_mode', 'plan_begin_date', 'plan_end_date'),
            ('has_env', 'final_env'),
            ('approve', 'approver', 'approve_date'),

            '-- 执行信息 --',
            ('status', 'real_begin_date', 'real_end_date'),
            ('test_contactor', 'qc_domain', 'qc_project'),
            
            '-- 其它信息 --',
            ('modified_date', 'modifield_user'),
            ]
            
    class ApprovePlanForm:
        fields = [
            {'name':'real_mode', 'required':True},
            {'name':'plan_begin_date', 'required':True},
            {'name':'plan_end_date', 'required':True},
            'appr',
            'reason',
        ]
        layout = [
            '-- 测试计划审批 --',
            'real_mode', 
            ('plan_begin_date', 'plan_end_date'),
            'appr',
            'reason',
        ]
        
    class ApproveEnvForm:
        fields = [
            {'name':'has_env', 'required':True},
            'final_env',
            'appr',
            'reason',
        ]
        layout = [
            '-- 测试环境审批 --',
            'has_env', 
            'final_env', 
            'appr',
            'reason',
        ]
        
    class ExecuteForm:
        fields = [
            'status', 'real_begin_date', 'real_end_date',
            'test_contactor', 'qc_domain', 'qc_project',
        ]
        layout = [
            ('status', 'test_contactor'),
            ('real_begin_date', 'real_end_date'),
            ('qc_domain', 'qc_project'),
        ]

    class AddForm:
        fields = [
            'requirement',
            'tx',
            'depart',
            'system',
            'sub_system',
            'project_name',
            'desc',
            'version_date',
            'type',
            'mode',
            'origin_begin_date',
            'origin_end_date',
            'test_systems',
            'rel_systems',
            'use_kfzx_env',
            'use_deployed_env',
            {'name':'new_env', 'verbose_name':'新环境需求'},
            'contactor',
            'memo',
        ]
        
    class ChangeForm:
        fields = ['sub_system', 'desc', 'type', 'mode', 
            'project_name', 'plan_begin_date', 'plan_end_date',
            'test_systems', 'rel_systems', 
            'use_kfzx_env', 'use_deployed_env',
            {'name':'new_env', 'verbose_name':'新环境需求'}, 'contactor', 'memo']
        layout = [{'name':'sub_system', 'colspan':3}, 
            'desc', 
            ('type', 'mode', 'project_name'),
            ('plan_begin_date', 'plan_end_date'),
            'test_systems', 'rel_systems', 
            ('use_kfzx_env', 'use_deployed_env'),
            'new_env', 'contactor', 'memo']
    
    def get_receiver_ids(self, sender_id):
        users = set([self._creator_])
        User = get_model('user')
        test_user = User.filter(User.c.department =='3')
        
        for x in self.systems.all():
            users.update(set(x.users.ids()))
        if sender_id in users:
            users.remove(sender_id)
        for row in test_user:
            if row.id in users:
                users.remove(row.id)
            
        return list(users)
    
    def __unicode__(self):
        return "%s-%s" % (self.test_id,self.title)
    
    def get_url(self):
        return u'<a href="/tests/plans/view/%d" title="%s">%s</a>' % (self.id, self.title, self.test_id)
    
class TestPlanComment(Model):
    type = Field(CHAR, max_length=1, verbose_name='审批类型', choices=[('1', '计划审批'), ('2', '环境审批'), ('3', '变更审批')])
    test_task = Reference('testplan', verbose_name='关联测试任务', collection_name='comments')
    approver = Reference('user', verbose_name='审批人', collection_name='approver_test_tasks_comments')
    approve_date = Field(datetime.date, verbose_name='审批时间')
    comment = Field(TEXT, verbose_name='审批意见')
    approve = Field(CHAR, max_length=1, verbose_name='审批结果', choices=get_var('PARA/TEST_APPROVE'))
    
class TestChangeRequest(Model):
    test_task = Reference('testplan', verbose_name='关联测试任务', collection_name='testplan_change_requests')
    old_value = Field(PICKLE, verbose_name='原信息')
    new_value = Field(PICKLE, verbose_name='新信息')
    submitter = Reference('user', verbose_name='提交人', collection_name='submitter_changes')
    submit_date = Field(datetime.date, verbose_name='提交时间')
    approver = Reference('user', verbose_name='审批人', collection_name='approver_changes')
    approve_date = Field(datetime.date, verbose_name='审批时间')
    approve = Field(CHAR, max_length=1, verbose_name='审批结果', choices=get_var('PARA/TEST_APPROVE_STATUS'))
    
    class Table:
        fields = [
            {'name':'test_task', 'width':250, 'sortable':True},
            {'name':'approve', 'width':60, 'sortable':True},
            {'name':'submitter', 'width':100, 'sortable':True},
            {'name':'submit_date', 'width':100, 'sortable':True},
            {'name':'approver', 'width':60, 'sortable':True},
            {'name':'approve_date', 'width':100, 'sortable':True},
        ]
        
class VersionVerificationTest(Model):
    system = Reference('system', verbose_name='系统',collection_name='sys_vertion_test', required=True)
    sys_abbr = Field(CHAR, verbose_name='英文简写', max_length=20)
    begin_date = Field(datetime.date, verbose_name='计划开始时间')
    end_date = Field(datetime.date, verbose_name='计划结束时间')
    version_date = Field(datetime.date, verbose_name='投产时间')
    mode = Field(CHAR, max_length=1, choices=get_var('PARA/TEST_MODE'), verbose_name='测试实施模式', required=True)
    modified_date = Field(datetime.datetime, verbose_name='修改时间', auto_now=True, auto_now_add=True)
    modifield_user = Reference('user', verbose_name='修改人', default=get_modified_user, auto=True, auto_add=True)
    
class TestPlan_Functions(Model):
    testplan = Reference('testplan', verbose_name='测试计划', required=True, collection_name='testplan_functions')
    task = Reference('tasks_develop', verbose_name='对应任务', required=True)
    function = Reference('sysfunction', verbose_name='系统功能点', required=True)
    change_status = Field(CHAR, verbose_name='变动状态', max_length=1, choices=get_var('PARA/CHANGE_STATUS'), required=True)
    open_status = Field(CHAR, max_length=1, verbose_name='开放使用状态', choices=get_var('PARA/YES_NO'))
    open_secverify = Field(CHAR, max_length=1, verbose_name='开放验密需求', choices=get_var('PARA/YES_NO'))
    test_point = Field(str, max_length=255, verbose_name='功能测试要点')
    update_date = Field(datetime.datetime, verbose_name='更新时间')
    update_user = Reference('user', verbose_name='更新人', default=get_modified_user, collection_name='user_testplan_functions')
    modified_date = Field(datetime.datetime, verbose_name='修改时间', auto_now=True, auto_now_add=True)
    modified_user = Reference('user', verbose_name='修改人', default=get_modified_user, auto=True, auto_add=True)
    kfzx = Reference('kfzx', verbose_name='开发中心')
    system = Reference('system', verbose_name='系统', collection_name='sys_testplan_functions')
    
    #以下为系统来修改
    is_executed = Field(CHAR, max_length=1, verbose_name='是否执行测试', choices=get_var('PARA/YES_NO'))
    test_result = Field(CHAR, max_length=1, verbose_name='功能测试结果', choices=get_var('PARA/YES_NO'))
    test_finish_date = Field(datetime.datetime, verbose_name='测试完成时间')
    
    @classmethod
    def OnInit(cls):
        Index('tasks_funcs_indx', cls.c.testplan, cls.c.task, cls.c.function)
    
    def __unicode__(self):
        return unicode(self.function)
    
    class Table:
        fields = [
#            {'name':'testplan', 'width':60},
            {'name':'category', 'verbose_name':'分类', 'width':60, 'sortable':True},
            {'name':'task', 'width':150, 'sortable':True},
            {'name':'function_code', 'width':100, 'verbose_name':'功能代码'},
            {'name':'function', 'width':250, 'sortable':True},
            {'name':'change_status', 'width':60, 'sortable':True},
            {'name':'open_status', 'width':60, 'sortable':True},
            {'name':'open_secverify', 'width':60, 'sortable':True},
            {'name':'test_point', 'width':80, 'sortable':True},
            {'name':'is_executed', 'width':80, 'sortable':True},
            {'name':'test_result', 'width':80, 'sortable':True},
            {'name':'test_finish_date', 'width':80, 'sortable':True},
            {'name':'update_date', 'width':80},
            {'name':'update_user', 'width':80},
            {'name':'kfzx', 'width':80},
            {'name':'system', 'width':80},
        ]
    
class TestEnv(Model):
    testplan = Reference('testplan', verbose_name='测试计划', collection_name="testplan_envs")
    system = Reference('system', verbose_name='所属系统')
    category = Field(CHAR, max_length=1, verbose_name='环境分类', choices=get_var('PARA/TEST_ENV_CATEGORY'))
    type = Field(CHAR, max_length=2, verbose_name='型号', choices=get_var('PARA/TEST_ENV_TYPE'))
    amount = Field(int, verbose_name='数量')
    unit = Field(int, verbose_name='单位数量')